var placeT = new Array();
var Phouse = new Array();

BindKey(KEY_TILDE , "", "");

var Sphere_Audio = new Object;

with (Sphere_Audio) {
  Music = undefined;
  Sounds = new Array(16);
  CurrentSound = 0;
}

function ChangeMusic(filename)
{
Battle.counter = 0;
Battle.engage = false;
  with (Sphere_Audio) {
		if (filename == ""){
		Music.stop();
		}
    if (Music != undefined) {
     Music.stop();
		}
   
    if (filename.length != 0) {
     Music = LoadSound(filename);
     Music.play(true);
    }
  }
}


function PlaySound(filename)
{
  with (Sphere_Audio) {
  
    // stop the current sound
    if (Sounds[CurrentSound] != undefined) {
      Sounds[CurrentSound].stop();
    }
    
    // load new one
    Sounds[CurrentSound] = LoadSound(filename);
    Sounds[CurrentSound].play(false);
  
    // go to the next sound
    CurrentSound++;
    if (CurrentSound >= Sounds.length) {
      CurrentSound = 0;
    }
  }
}

function LaughPerson(name, keeplaugh)
{
Player.equipweptemp = name;
Player.temp
DetachInput();
BindKey(KEY_ESCAPE, "", "");
SetPersonDirection(name, 8)
SetPersonFrame(name, 6)
SetDelayScript(10, 'SetPersonFrame(Player.equipweptemp, 7)');
SetDelayScript(20, 'SetPersonFrame(Player.equipweptemp, 6)');
SetDelayScript(30, 'SetPersonFrame(Player.equipweptemp, 7)');
SetDelayScript(40, 'SetPersonFrame(Player.equipweptemp, 6)');
SetDelayScript(50, 'SetPersonFrame(Player.equipweptemp, 7)');
SetDelayScript(60, 'SetPersonFrame(Player.equipweptemp, 6)');
SetDelayScript(70, 'SetPersonFrame(Player.equipweptemp, 7)');
  if (keeplaugh == "yes")
  {
  }
  else
  {
  SetDelayScript(100, 'SetPersonDirection(Player.equipweptemp, 4)')
  SetDelayScript(100, 'SetPersonFrame(Player.equipweptemp, 0)')
  }
SetDelayScript(100, 'AttachInput("main character")');
SetDelayScript(100, 'BindKey(KEY_ESCAPE, "GameMenu()", "")');
}

function FingerAni(name, keep)
{
Player.equipweptemp = name;
Player.temp
DetachInput();
BindKey(KEY_ESCAPE, "", "");
Player.dir = GetPersonDirection(name);
Player.frame = GetPersonframe(name);

SetPersonDirection(name, 8)
SetPersonFrame(name, 5)

SetDelayScript(10, 'SetPersonFrame(Player.equipweptemp, 4)');
SetDelayScript(20, 'SetPersonFrame(Player.equipweptemp, 5)');
SetDelayScript(30, 'SetPersonFrame(Player.equipweptemp, 4)');
SetDelayScript(40, 'SetPersonFrame(Player.equipweptemp, 5)');

  if (keep == "yes")
  {
  }
  else
  {
  SetDelayScript(60, 'SetPersonDirection(Player.equipweptemp, Player.dir)')
  SetDelayScript(60, 'SetPersonFrame(Player.equipweptemp, Player.frame)')
  }
SetDelayScript(60, 'AttachInput("main character")');
SetDelayScript(60, 'BindKey(KEY_ESCAPE, "GameMenu()", "")');
}
function FingerAni(name, keep)
{
Player.equipweptemp = name;
Player.temp
DetachInput();
BindKey(KEY_ESCAPE, "", "");
Player.dir = GetPersonDirection(name);
Player.frame = GetPersonFrame(name);

SetPersonDirection(name, 8)

SetDelayScript(10, 'SetPersonFrame(Player.equipweptemp, 3)');
SetDelayScript(20, 'SetPersonFrame(Player.equipweptemp, 4)');
SetDelayScript(30, 'SetPersonFrame(Player.equipweptemp, 3)');
SetDelayScript(40, 'SetPersonFrame(Player.equipweptemp, 4)');

  if (keep == "yes")
  {
  }
  else
  {
  SetDelayScript(60, 'SetPersonDirection(Player.equipweptemp, Player.dir)')
  SetDelayScript(60, 'SetPersonFrame(Player.equipweptemp, Player.frame)')
  }
SetDelayScript(60, 'AttachInput("main character")');
SetDelayScript(60, 'BindKey(KEY_ESCAPE, "GameMenu()", "")');
}
function NodAni(name, LR)
{
Player.equipweptemp = name;
Player.temp
DetachInput();
BindKey(KEY_ESCAPE, "", "");
Player.dir = GetPersonDirection(name);
Player.frame = GetPersonFrame(name);

if (LR == "North")
{
SetDelayScript(10, 'SetPersonDirection(Player.equipweptemp, 8)');
SetDelayScript(10, 'SetPersonFrame(Player.equipweptemp, 8)');
SetDelayScript(20, 'SetPersonDirection(Player.equipweptemp, 0)');
}
else if (LR == "East")
{
SetDelayScript(10, 'SetPersonDirection(Player.equipweptemp, 8)');
SetDelayScript(10, 'SetPersonFrame(Player.equipweptemp, 11)');
SetDelayScript(20, 'SetPersonDirection(Player.equipweptemp, 2)');
}
else if (LR == "West")
{
SetDelayScript(10, 'SetPersonDirection(Player.equipweptemp, 8)');
SetDelayScript(10, 'SetPersonFrame(Player.equipweptemp, 10)');
SetDelayScript(20, 'SetPersonDirection(Player.equipweptemp, 6)');
}
else if (LR == "South")
{
SetDelayScript(10, 'SetPersonDirection(Player.equipweptemp, 8)');
SetDelayScript(10, 'SetPersonFrame(Player.equipweptemp, 5)');
SetDelayScript(20, 'SetPersonDirection(Player.equipweptemp, 4)');
}
SetDelayScript(20, 'SetPersonFrame(Player.equipweptemp, 0)');

SetDelayScript(40, 'AttachInput("main character")');
SetDelayScript(40, 'BindKey(KEY_ESCAPE, "GameMenu()", "")');
}

function TorkRHSpecial1()
{
  RenderMap();
  WindowStyle.drawWindow (25, 380, 590, 80);
  Font.drawTextBox (75, 400, 525, 50, 5, "Bill: Hello, my name is Billy Bob Pete, and I was wondering if you adventures would mind helping me clear the rats out my house.  Interested?");

  // create the menu object
  var PubMenu = new Menu();
  PubMenu.arrow           = Arrow;
  PubMenu.font            = Font;
  PubMenu.window_style    = WindowStyle;
  PubMenu.escape_function = InnmenuNo;

  // add the commands to it
  PubMenu.addItem("Sure",     RHmenuYes);
  PubMenu.addItem("Nah",    RHmenuNo );
  PubMenu.execute(515, 350, 100, 20);
}



function TorkMagnarSpecial1()
{
  if (Player.chrclass == "Mage")
  {
  SetPersonDirection("main character", 0);
  SetPersonDirection("Magnar", 4);
  Talk_Window("Magnar: " + Player.name + "!  You're finally here!  I was starting to get worried.  Do you have the herbs?", "blank.png");
  Delay(200);
  MiscPersonMove("main character", COMMAND_MOVE_NORTH, COMMAND_FACE_NORTH, 36, 0);
  QueuePersonCommand("main character", COMMAND_FACE_EAST, false);
  SetDelayScript(38, "TorkMagnarSpecial2()");
  }
}
function TorkMagnarSpecial2()
{
  SetPersonFrame("main character", 0);
  SetPersonDirection("Magnar", 6);
  Talk_Window(Player.name + ": Don't worry, I got everything you need right here.", Player.port);
  Delay(200);
  Talk_Window("Magnar: Excellent!  Thank you very much.  Oh yes, these will do very nicely, thanks again.", "blank.png")
  Delay(200);
  Talk_Window(Player.name + ": You're welcome, but what are you going to do with them anyway?", Player.port);
  Delay(200);
  Talk_Window("Magnar: I am going to make a powerful potion out of it.  What kind?  Oh, you will see in time but for now let's just go and watch the tournament.", "blank.png")
  Delay(200);
  Talk_Window(Player.name + ": Sorry but when I went to come here I didn't plan on watching so I don't have any tickets.", Player.port);
  Delay(200);
  Talk_Window("Magnar: That doesn't really matter if you know the new chairperson of the tournament.  I'll get us the best seats in the house.", "blank.png")
  Delay(200);
  Talk_Window(Player.name + ": You're the new chairperson?!  I heard that there was a new one, but I didn't know it was you!", Player.port);
  Delay(200);
  Talk_Window("Magnar:  Yes, I am the new chairperson, but let's not waste time standing around here.  Let us go to the tournament!", "blank.png")
  Delay(200);
 
DetachInput();
RenderMap();
FadeToColor(500, Black);
ChangeMap("TorkRing.rmp");
CreatePerson("Magnar", "Magnar.rss", true);
SetPersonX("Magnar",504);
SetPersonY("Magnar",488);
SetPersonLayer("Magnar", 2);
SetPersonDirection("Magnar", 4);

SetPersonX("main character", 472);
SetPersonY("main character",488);
SetPersonLayer("main character", 2);
SetPersonDirection("main character", 4);

CreatePerson("Dagnar", "Dagnar.rss", true);
SetPersonX("Dagnar", 536);
SetPersonY("Dagnar",488);
SetPersonLayer("Dagnar", 2);
SetPersonDirection("Dagnar", 4);

SetCameraX(504);
SetCameraY(648);
RenderMap();
FadeFromColor(500, Black);
eatKey();
Delay(500);
RangerTournament2();
}

function BardSong1()
{
Verse("Orex Pangara, 860, my dear and loved son Tom", "How are you doing?  I miss you much,", "Though it's only been a few weeks", "It's so sad what a man must do:", "To go to Trimere for a job to seek");
Verse("But how is it going? Are you working?", "I know you'll live a good life", "But your mother keeps worrying about you", "and fears you'll feel nothing but strife", "");
RenderMap();
Font.drawText (0, 0, "");
FlipScreen();
Delay(200);
Verse("Orex Pangara, 870, my dear and loved son Tom", "I got your letter the other day", "and I'm happy you're doing so well", "But my arthritis has gotten worse", "As you can already tell");
Verse("Your bother Michael came for a visit", "And we talked all through the night", "We hope you'll do the same soon", "And please know that any time's right", "");
RenderMap();
Font.drawText (0, 0, "");
FlipScreen();
Delay(200);
Verse("Orex Pangara, 880, my dear and loved son Tom", "I'm sorry to write this I know it'll hurt", "But your dear old father has died", "The funeral was sad and o' so quaint", "And even your dear sister cried");
Verse("The house feels so lonely without him", "and everything just makes me so sad", "Why don't you come back for a visit", "And talk about your dear old dad?", "");
RenderMap();
Font.drawText (0, 0, "");
FlipScreen();
Delay(200);
Verse("Orex Pangara, 890, my dear and loved son Tom", "Oh Tom, I'm still thinking about you", "And why you haven't come around", "I wish you knew how much I missed you", "Since you packed up and left our town");
Verse("I know you're busy with your job", "And preparing your kids for life", "But why can't you come back for visit", "Along with your lovely young wife?", "");
RenderMap();
Font.drawText (0, 0, "");
FlipScreen();
Delay(200);
Verse("Orex Pangara, 994, my dear bother Tom", "I know you loved our mother dearly", "And I'm sadden to say that she's gone", "But it's harder to say how lonely she was", "In the years before she passed on.");
Verse("We buried her along side of father", "Down by the cemetery's end", "But though she's gone, why not visit?", "We'd all love to see you again", "");
}
function Verse(l1, l2, l3 ,l4, l5)
{
 RenderMap(); 
 WindowStyle.drawWindow (100, 385, 440, 60);
 if (l5 == "")
 {
 Font.drawText (125, 395, l1);
 Font.drawText (125, 405, l2);
 Font.drawText (125, 415, l3);
 Font.drawText (125, 425, l4);
 }
 else
 {
 Font.drawText (125, 390, l1);
 Font.drawText (125, 400, l2);
 Font.drawText (125, 410, l3);
 Font.drawText (125, 420, l4);
 Font.drawText (125, 430, l5);
 }
 FlipScreen();
 eatKey()
 while (IsKeyPressed(KEY_SPACE) == false) 
 {Delay(100);}
 {GetKey()}
}

function IntroText(text, text2, text3, textx, texty, texty2, texty3, textxlong, textylong, textsize, textdelay, scrollamount, pixelamount)
{
 for (var i = 0; i < scrollamount/pixelamount; i++) 
 {
 Font.drawTextBox (textx, texty - (i * pixelamount), textxlong, textylong, textsize, text);
 Font.drawTextBox (textx, texty2 - (i * pixelamount), textxlong, textylong, textsize, text2);
 Font.drawTextBox (textx, texty3 - (i * pixelamount), textxlong, textylong, textsize, text3);
 FlipScreen();
 Delay(textdelay);
 }
}

function Talk_Window2(TalkString, port)
{
 RenderMap() 
 WindowStyle.drawWindow (25, 380, 590, 80);//25, 330, 590, 130
 Portrait = LoadImage(port);
 Portrait.blit(30, 390);
 Font.drawTextBox (75, 390, 525, 60, 5, TalkString);//75, 380, 525, 100, 5
 FlipScreen();
 eatKey()
 while (IsKeyPressed(KEY_SPACE) == false) 
 {Delay(100);}
 {GetKey()}
}


function Delay(milliseconds)
{
  var start = GetTime();
  while (start + milliseconds > GetTime()) {
  }
  if (Player.MapEnginePres == true)
  {
  SetDelayScript(10, "Blank()");
  }
}

function Blank()
{
}

function IntroText1()
{
  IntroText("                                 Resorsis", "  942 A.F., Nine hundred years after the discovery of magic.  The world has changed since its discovery and the birth of the age of logic is beginning.  Already the 'magics' of logic are starting to see the light - the printing press, machines that work by themselves, and the discovery of electricity have all pushed the world forward.  But magic has not died.  Since its beginning it has expanded and now the knowledge of its use has become well known, though still mastered by only a select few.  Yet these two forces have not collided, the domain that each possesses in the world are seperate and a balance between them has occurred.  However, this balance could shift as a new age of darkness and evil is coming that has the potential to reshape the earth and the lives of those who live there.", "  But with this comes a warrior.  A warrior with the ability to change the world and its fate.  But who is this person?  And what name will history forever call this warrior?", 25, 490, 510, 620, 590, 300, 5, 25, 655, 1);
}

function IntroText2()
{
  NewMap("world_map.rmp", 872, 1208, 1, 2);
  eatKey();
}

function ZienEnter1()
{
  if (Player.event9 == 0)
  {
    if (Player.chrclass == "Soldier")
    {
    ZienEnter2();
    }
    else if (Player.chrclass == "Ranger")
    {
    ZienEnter2();
    }
  }
}

function ZienEnter2()
{
  DetachInput();
  BindKey(KEY_ESCAPE, "", "");
  MiscPersonMove("main character", COMMAND_MOVE_WEST, COMMAND_FACE_WEST, 177, 0);
  MiscPersonMove("main character", COMMAND_MOVE_NORTH, COMMAND_FACE_NORTH, 80, 0);
  SetDelayScript(273, 'ZienEnter2_5()');
  SetDelayScript(289, 'MiscPersonMove("main character", COMMAND_MOVE_NORTH, COMMAND_FACE_NORTH, 16, 0)');
  SetDelayScript(305, 'ZienEnter3()');
}
function ZienEnter2_5()
{
Player_x = GetPersonX("main character");
Player_y = GetPersonY("main character");
Player_x_tile = Math.floor(Player_x/16);
Player_y_tile = Math.floor(Player_y/16);
  PlaySound("click.wav");
  SetTile(Player_x_tile, Player_y_tile - 1, 1, 9);
  SetTile(Player_x_tile, Player_y_tile - 2, 1, 8);
}
function ZienEnter3()
{
  RenderMap();
  FadeToColor(500, Black);
  ChangeMap("ZienArm.rmp")
  SetPersonX("main character", 1176);
  SetPersonY("main character", 1016);
  SetCameraX(1176);
  SetCameraY(1016);

  SetPersonLayer("main character", 1);
  CreatePerson("Dagnar", "Dagnar.rss", true);
  SetPersonX("Dagnar", 1176);
  SetPersonLayer("Dagnar", 1);
  SetPersonY("Dagnar", 1032);
  SetPersonDirection("main character", 0);
  RenderMap();
  FadeFromColor(500, Black);
  MiscPersonMove("main character", COMMAND_MOVE_NORTH, COMMAND_FACE_NORTH, 16, 1);
  MiscPersonMove("main character", COMMAND_MOVE_WEST, COMMAND_FACE_WEST, 16, 1);
  MiscPersonMove("main character", COMMAND_MOVE_NORTH, COMMAND_FACE_NORTH, 48, 1);

  SetDelayScript(162, 'SetPersonFrame("main character", 0)');
  SetDelayScript(48, 'MiscPersonMove("Dagnar", COMMAND_MOVE_WEST, COMMAND_FACE_NORTH, 2, 1)');
  SetDelayScript(48, 'MiscPersonMove("Dagnar", COMMAND_MOVE_NORTH, COMMAND_FACE_NORTH, 24, 1)');
  SetDelayScript(48, 'MiscPersonMove("Dagnar", COMMAND_MOVE_WEST, COMMAND_FACE_WEST, 48, 1)');
  SetDelayScript(48, 'MiscPersonMove("Dagnar", COMMAND_MOVE_NORTH, COMMAND_FACE_NORTH, 72, 1)');
  SetDelayScript(324, "ZienEnter4()");
}
function ZienEnter4()
{
SetPersonFrame("main character", 0);
SetPersonFrame("Dagnar", 0);
Talk_Window("General: Well, what have we here?  " + Player.name + " and Dagnar.  What a wonderful surprise!  Here's your cheques, now get out.  I have work to do.", "blank.png"); 
Delay(200);
Talk_Window("Received 1000gp!", "blank.png");
Delay(200);
Player.gp = Player.gp + 1000;
Talk_Window(Player.name + ":  Uh, sir, we didn't come here just for our cheques...", Player.port); 
Delay(200);
Talk_Window("General: Is that so?  Well then speak up!  I haven't got all day here.", "blank.png"); 
Delay(200);
Talk_Window(Player.name + ": The new chairperson of Tork's Tournament Committee has asked for our assistance in capturing and bringing back a thief who has stolen something of extreme value to him and the Tournament Committee.", Player.port); 
Delay(200);
Talk_Window("General: And how much time do you want off in order to do this task of his?", "blank.png"); 
Delay(200);
Talk_Window("Dagnar: It should take only a few weeks to do, things could pop up though...", Player2.port); 
Delay(200);
Talk_Window(Player.name + ": We would really appreciate it if you would grant us the time sir.", Player.port); 
Delay(200);
Talk_Window("General: Fine then, I will give you leave to do whatever this thing is that you want to do.", "blank.png"); 
Delay(200);
Talk_Window(Player.name + ": Thank you sir!", Player.port); 
Delay(200);
Talk_Window("General: By the way, you're fired.", "blank.png"); 
Delay(200);
SetPersonDirection("main character", 8);
SetPersonFrame("main character", 12);
SetPersonDirection("Dagnar", 8);
SetPersonFrame("Dagnar", 12);

Talk_Window(Player.name + ": What?!", Player.port); 
Delay(200);
SetPersonDirection("main character", 0);
SetPersonFrame("main character", 0);
SetPersonDirection("Dagnar", 0);
SetPersonFrame("Dagnar", 0);
Talk_Window("General: " + Player.name + ", don't take this too hard, you were a fine recruit when we first hired you.  But now your work is slopy and you take leave almost every second week!", "blank.png"); 
Delay(200);
Talk_Window(Player.name + ": But I fixed the problem in Pire Forest just a short while ago.", Player.port); 
Delay(200);
if (Player.chrclass == "Soldier")
{
Talk_Window("General: I don't care what you did you're still fired!  Now take off your uniform that you've discraced so much and leave.  Perhaps someone else will serve it more justice in the future.", "blank.png"); 
Delay(200);
Player.sprite = "soldierchrclass.rss"; 
}
else
{
Player.sprite = "Ranger.rss"; 
Talk_Window("General: Well it's about time.  You were susposed to do that four weeks ago!", "blank.png"); 
Delay(200);
Talk_Window("General: Look " + Player.name + ", I know this must be hard for you, but the military of Hettra doesn't want or need someone like you.  Now please leave.", "blank.png"); 
Delay(200);
}
Talk_Window(Player.name + ": Yes sir.", Player.port); 
Delay(200);

  RenderMap();
  FadeToColor(500, Black);
  ChangeMap("ZienMain.rmp")
  DetachCamera("main character");
  DestroyPerson("main character")
  CreatePerson("main character", Player.sprite, false);
  SetPersonX("main character", 680);
  SetPersonY("main character", 392);
  AttachCamera("main character");
  SetCameraX(680);
  SetCameraY(392);
  SetPersonLayer("main character", 1);
  SetPersonDirection("main character", 4);
  RenderMap();
  FadeFromColor(500, Black);

Player.event9 = 1;
  if (Player.chrclass == "Soldier")
  {
  Player.port = "Soldier2.png";
  Talk_Window(Player.name + ": Haven't worn these in a while...", Player.port);
  }
AttachInput("main character");
BindKey(KEY_ESCAPE, "GameMenu()", "");
}

function GarretJoin()
{
Player3.pres = true;
Player3.name = "Garret"
Player3.level = 5;
Player3.nextlevel = 2073;
Player3.exp = 1853;

Player3ranger.animals = true;
Player3ranger.heal = false;
Player3ranger.heal2 = false;
Player3ranger.heal3 = false;
Player3ranger.defup = false;
Player3ranger.powerup = false;
Player3ranger.evade = false;

Player.quest = "Travel to Glace and track the thief";

Player3.hp = 65;
Player3.maxhp = 65;
Player3.mp = 17;
Player3.maxmp = 17;
Player3.str = 34;
Player3.agi = 30;
Player3.mag = 16;
Player3.vit = 20;
Player3.luck = 15;

Player3.chrclass = "Ranger";
Player3.port = "Garret.png";
Player3.spriteset = "Garret.rss";
Player3.equipwep = "Long Sword";
Player3.equiparmour = "Studded Leather Armour";

Player3.wepeffect = 25;
Player3.attack = Player3.str + 25;
Player3.defence = Player3.vit + 25;
Player3.mattack = Player3.mag;
Player3.evade = Math.floor(Player3.agi/5);

Player3.weaponeffect = 25;
Player3.armoureffect = 25;
Player3.mattackeffect = 1;
Player3.weaponthing = "Weapon";

}

function ZienGarret1()
{
FacePlayer("ZienGarret")
Talk_Window("Garret: What do you want?", "Garret.png");
Delay(200);
Talk_Window(Player.name + ": Are you Garret?", Player.port);
Delay(200);
LaughPerson("ZienGarret", "yes")

SetDelayScript(100, 'ZienGarret2()');

}

function ZienGarret2()
{

Talk_Window("Garret:  Yes this is the Garret the Great, how may I help you?", "Garret.png");
Delay(200);
SetPersonDirection("ZienGarret", 4)
SetPersonFrame("ZienGarret", 0)
Talk_Window(Player.name + ": A person in Tork called Magnar wanted us to see you.  Someone has stolen a potion from him and he sent us to find whoever it was.  He said that you could help.", Player.port);
Delay(200);
Talk_Window("Garret: Why Master Magnar would give such a task to you two boneheads is beyond me.  But since he asked you to seek my help I guess he's still thinking straight. ", "Garret.png");
Delay(200);
Talk_Window(Player.name + ": So are you up for the job?", Player.port);
Delay(200);
Talk_Window("Garret:  Of course.", "Garret.png");
Delay(200);
Talk_Window("Garret has joined the party!", "blank.png");
Delay(200);
Talk_Window(Player.name + ": Great.  The man we're looking for has a dark cape and hair that's been bleached white.  Have you seen anyone like that lately?", Player.port);
Delay(200);
Talk_Window("Garret: Bleached white hair?  No, I'm pretty sure no one like that came across here.  If he did come by he would have stuck out like a sore thumb.", "Garret.png");
Delay(200);
Talk_Window(Player.name + ": In that case he must have bypassed Zien and went straight to Glace, probably took a short cut through the woods just north of here.  ", Player.port);
Delay(200);
Talk_Window("Garret: So it's off to Glace to find this.. thief eh?  Very well, let's go.", "Garret.png");
Delay(200);
GarretJoin();
RenderMap();
FadeToColor(500, Black);
DestroyPerson("ZienGarret");
RenderMap();
Delay(300);
FadeFromColor(500, Black);
Player.event10 = 1;
}


//Person Movement code

function MovePersonNorth(name)
{
//Player_x = GetPersonX("main character");
//Player_y = GetPersonY("main character");
//Player_x_tile = Math.floor(Player_x/16);
//Player_y_tile = Math.floor(Player_y/16);

//This to prevent the person from walking over a door.

  if (GetTile(Math.floor(GetPersonX(name)/16), Math.floor(GetPersonY(name)/16 - 1) , GetPersonLayer(name)) == 153)
  {  
  }
  else if (GetTile(Math.floor(GetPersonX(name)/16), Math.floor(GetPersonY(name)/16 - 1) , GetPersonLayer(name)) == 9)
  {
  }
  else
  {
  MiscPersonMove(name, COMMAND_MOVE_NORTH, COMMAND_FACE_NORTH, 16, 1);
  }
}
function MovePersonSouth(name)
{
MiscPersonMove(name, COMMAND_MOVE_SOUTH, COMMAND_FACE_SOUTH, 16, 1);
}
function MovePersonWest(name)
{
MiscPersonMove(name, COMMAND_MOVE_WEST, COMMAND_FACE_WEST, 16, 1);
}
function MovePersonEast(name)
{
MiscPersonMove(name, COMMAND_MOVE_EAST, COMMAND_FACE_EAST, 16, 1);
}
function MovePersonWait(name)
{
MiscPersonMove(name, COMMAND_WAIT, "", 16, 0);
}

function RandomMovePerson(name, booy)
{
  if (Math.random() <= 0.20)
  {
  MovePersonEast(name);
  MovePersonWait(name);
  }
  else if (Math.random() <= 0.25)
  {
  MovePersonWest(name);
  MovePersonWait(name);
  }
  else if (Math.random() <= 0.33)
  {  
  MovePersonNorth(name);
  MovePersonWait(name);
  }
  else if (Math.random() <= 0.5)
  {  
  MovePersonSouth(name);
  MovePersonWait(name);
  }
  else
  {
  MovePersonWait(name);
  }
	if (booy == true)//Extra wait time for bosses.
	{
  MovePersonWait(name);
  MovePersonWait(name);
  MovePersonWait(name);
	}

}


function RandomMovePerson2(name)
{
Player.temp = name;

ClearPersonCommands(Player.temp);

  if (Math.random() <= 0.25)
  {
    if (IsPersonObstructed(Player.temp, GetPersonX(Player.temp) + 16, GetPersonY(Player.temp)) == false)
    {
      if (Player.movetemp == "west")
      {
        if (Player.movetemp1 >= 10)
        {
        MovePersonEast(name);
        Player.movetemp = "east";
        Player.movetemp1 = 0;
        }
        else
        {
        Player.movetemp1 = Player.movetemp1 + 1;
        }
      }
      else
      {
      MovePersonEast(name);
      Player.movetemp = "east";
      Player.movetemp1 = 0;
      }
    }
  }
  else if (Math.random() <= 0.33)
  {
    if (IsPersonObstructed(Player.temp, GetPersonX(Player.temp) - 16, GetPersonY(Player.temp)) == false)
    {  
      if (Player.movetemp == "east")
      {
        if (Player.movetemp1 >= 10)
        {
        MovePersonWest(name);
        Player.movetemp = "west";
        Player.movetemp1 = 0;
        }
        else
        {
        Player.movetemp1 = Player.movetemp1 + 1;
        }
      }
      else
      {
      MovePersonWest(name);
      Player.movetemp = "west";
      Player.movetemp1 = 0;
      }
    }
  }
  else if (Math.random() <= 0.5)
  {
    if (IsPersonObstructed(Player.temp, GetPersonX(Player.temp), GetPersonY(Player.temp) - 16) == false)
    {    
      if (Player.movetemp == "south")
      {
        if (Player.movetemp1 >= 10)
        {
        MovePersonNorth(name);
        Player.movetemp = "north";
        Player.movetemp1 = 0;
        }
        else
        {
        Player.movetemp1 = Player.movetemp1 + 1;
        }
      }
      else
      {
      MovePersonNorth(name);
      Player.movetemp = "north";
      Player.movetemp1 = 0;
      }
    }
  }
  else
  {
    if (IsPersonObstructed(Player.temp, GetPersonX(Player.temp), GetPersonY(Player.temp) + 16) == false)
    {    
      if (Player.movetemp == "north")
      {
        if (Player.movetemp1 >= 10)
        {
        MovePersonSouth(name);
        Player.movetemp = "south";
        Player.movetemp1 = 0;
        }
        else
        {
        Player.movetemp1 = Player.movetemp1 + 1;
        }        
      }
      else
      {
      MovePersonSouth(name);
      Player.movetemp = "south";
      Player.movetemp1 = 0;
      }
    }
  }
}

function GlaceExit1()
{
  RenderMap();
  FadeToColor(500, Black);
  FlipScreen();
  FlipScreen();
/*
  ChangeMusic("");
  PlaySound("Chapter1.mp3");
  Delay(200);
  IntroText("  The first chapter has ended, and " + Player.name + ", along with Dagnar and Garret, are now on the way to Pangara in search of the thief that stole Magnar's valuable potion during this year's tournament at Tork.  Perhaps the party will find some answers when they reach Pangara City, the capitol of Pangara - but then again, maybe they won't...", "The eastern roads have been travelled much lately, but still monsters come out from the plains and forests nearby to attack those who journey along its long path. Is this a sign of the uncoming danger that will be faced by the world or will it just pass and fade away as quickly as it appeared?  The future will reveal the answer, but for now humans must wait to see what fate has in store for them and their planet...", "", 25, 490, 550, 580, 590, 300, 5, 25, 610, 1);
  Delay(200);
*/  
  ChangeMap("world_map.rmp");
  SetPersonX("main character", 1768);
  SetPersonY("main character", 1480);
  SetPersonLayer("main character", 1);
  SetPersonDirection("main character", 2);
  SetCameraX(1768);
  SetCameraY(1480);
  RenderMap();
  FadeFromColor(500, Black);
  eatKey();
  Player.event11 = 1;
//  Talk_Window("This ends the demo.", "blank.png");
//  Exit();
}

function Talk_Window3(TalkString, port)
{
 RenderMap() 
 WindowStyle.drawWindow (25, 380, 590, 80);//25, 330, 590, 130
 Portrait = LoadImage(port);
 Portrait.blit(30, 400);
 Font.drawTextBox (75, 395, 525, 50, 5, TalkString);//75, 380, 525, 100, 5
 FlipScreen();
 eatKey()
 while (IsKeyPressed(KEY_SPACE) == false) 
 {Delay(100);}
 {GetKey()}
}

function Door2(layer)
{
Player_x = GetPersonX("main character");
Player_y = GetPersonY("main character");
Player_x_tile = Math.floor(Player_x/16);
Player_y_tile = Math.floor(Player_y/16);
  if (GetTile(Player_x_tile, Player_y_tile-1, layer) == 11)
  {
  }
  else
  {
  PlaySound("click.wav");
  SetTile(Player_x_tile, Player_y_tile-1, layer, 11);
  SetTile(Player_x_tile, Player_y_tile - 2, layer, 10);
  }
}
function BDoor(layer)
{
Player_x = GetPersonX("main character");
Player_y = GetPersonY("main character");
Player_x_tile = Math.floor(Player_x/16);
Player_y_tile = Math.floor(Player_y/16);
  if (GetTile(Player_x_tile, Player_y_tile-1, layer) == 14)
  {
  }
  else
  {
  PlaySound("click2.wav");
  SetTile(Player_x_tile, Player_y_tile-1, layer, 14);
  SetTile(Player_x_tile, Player_y_tile - 2, layer, 14);
  SetTile(Player_x_tile - 1, Player_y_tile-1, layer, 13);
  SetTile(Player_x_tile - 1, Player_y_tile - 2, layer, 12);
  SetTile(Player_x_tile + 1, Player_y_tile-1, layer, 16);
  SetTile(Player_x_tile + 1, Player_y_tile - 2, layer, 15);
  }
}
function BDoor2(layer)
{
Player_x = GetPersonX("main character");
Player_y = GetPersonY("main character");
Player_x_tile = Math.floor(Player_x/16);
Player_y_tile = Math.floor(Player_y/16);
  if (GetTile(Player_x_tile, Player_y_tile + 2, layer) == 14 || GetPersonDirection("main character") == 0 ||GetPersonDirection("main character") == 1 ||GetPersonDirection("main character") == 7)
  {
  }
  else
  {
  PlaySound("click2.wav");
  SetTile(Player_x_tile, Player_y_tile + 2, layer, 14);
  SetTile(Player_x_tile, Player_y_tile + 1, layer, 14);
  SetTile(Player_x_tile - 1, Player_y_tile + 2, layer, 13);
  SetTile(Player_x_tile - 1, Player_y_tile + 1, layer, 12);
  SetTile(Player_x_tile + 1, Player_y_tile + 2, layer, 16);
  SetTile(Player_x_tile + 1, Player_y_tile + 1, layer, 15);
  }
}

function LandLordSpecial()
{
DetachInput();
BindKey(KEY_ESCAPE, "", "")
//QueuePersonCommand("main character", COMMAND_MOVE_WEST, false);
MiscPersonMove("main character", COMMAND_MOVE_NORTH, COMMAND_FACE_NORTH, 112, 1);
SetDelayScript(225, 'SetPersonDirection("main character", 6)');
SetDelayScript(225, 'SetPersonFrame("main character", 0)');
SetDelayScript(225, 'LandLordSpecial2()');
}

function LandLordSpecial2()
{
SetPersonFrame("main character", 0);
Delay(100);
RenderMap();
SetPersonDirection("LandLordP1", 8);
SetPersonFrame("LandLordP1", 11);
Delay(200);
Talk_Window("Man: That much for that house!?  You gotta be kidding!", "blank.png");
Delay(200);
RenderMap();
SetPersonDirection("LandLordP1", 0);
SetPersonFrame("LandLordP1", 0);
Delay(200);
Talk_Window("Real Estater:  This is prime real estate!  I would be a fool to sell it any lower!", "blank.png");
Delay(200);
Talk_Window("Man: Fine then, go ahead and over charge your customers!  I'll go somewhere else!", "blank.png");
Delay(200);
MiscPersonMove("LandLordP1", COMMAND_MOVE_EAST, COMMAND_FACE_EAST, 64, 1);
MiscPersonMove("LandLordP1", COMMAND_MOVE_SOUTH, COMMAND_FACE_SOUTH, 154, 1);
SetDelayScript(71, 'MiscPersonMove("main character", COMMAND_MOVE_EAST, COMMAND_FACE_WEST, 16, 1)');
SetDelayScript(154, 'SetPersonDirection("main character", 4)');
SetDelayScript(412, 'PlaySound("click.wav")');
SetDelayScript(412, 'DestroyPerson("LandLordP1")');
SetDelayScript(442, 'AttachInput("main character")');
SetDelayScript(442, 'BindKey(KEY_ESCAPE, "GameMenu()", "")');
Player.event14 = 1;
}